import { request } from '@/utils/request'
import type {
  AttrGroupResponseData,
  PostData,
  RelationResponseData,
} from '@/api/product/attrgroup/type.ts'

enum API {
  // 获取分类列表
  ATTRGROUP_LIST_URL = '/api/product/attrgroup/list',
  // 获取属性列表
  RELATION_LIST_URL = '/api/product/attrgroup',
  // 获取属性信息
  ATTRGROUP_INFO_URL = '/api/product/attrgroup/info',
  // 获取属性信息
  ATTRGROUP_DELETE_URL = '/api/product/attrgroup/delete',
  // 属性分类删除
  RELATION_DELETE_URL = '/api/product/attrgroup/attr/relation/delete',
  // 添加属性关联
  RELATION_ADD_URL = '/api/product/attrgroup/attr/relation',
  // 移除关联
  RELATION_DEL_URL = '/api/product/attrgroup/attr/relation/delete',
}

// 获取分类信息
export const reqAttrGroupList = (
  catId: number,
  page: number,
  limit: number,
  key: string,
) =>
  request.get<any, any>(
    API.ATTRGROUP_LIST_URL + `/${catId}?page=${page}&limit=${limit}&key=${key}`,
  )

// 获取属性列表
export const reqRelationList = (
  attrGroupId: number,
  page: number,
  limit: number,
  key: string,
) =>
  request.get<any, RelationResponseData>(
    API.RELATION_LIST_URL +
      `/${attrGroupId}` +
      '/noattr/relation' +
      `?page=${page}&limit=${limit}&key=${key}`,
  )

// 获取属性信息
export const reqAttrGroup = (attrGroupId: number) =>
  request.get<any, AttrGroupResponseData>(
    API.ATTRGROUP_INFO_URL + `/${attrGroupId}`,
  )

// 添加属性信息
export const reqAddResponseData = (
  url: string,
  attrGroupId: number,
  attrGroupName: string,
  sort: number,
  descript: string,
  icon: string,
  catelogId: number,
) => {
  return request.post<any, any>(API.RELATION_LIST_URL + `/${url}`, {
    attrGroupId,
    attrGroupName,
    sort,
    descript,
    icon,
    catelogId,
  })
}

// 删除属性信息
export const reqDeleteResponseData = (ids: any) => {
  return request.post<any, any>(API.ATTRGROUP_DELETE_URL, ids)
}

// 删除属性分类
export const reqDeleteRelationResponseData = (postData: PostData[]) => {
  return request.post<any, any>(API.RELATION_DELETE_URL, postData)
}

// 属性分类初始化
export const reqAttrInit = (attrGroupId: number) =>
  request.get<any, any>(
    API.RELATION_LIST_URL + `/${attrGroupId}` + '/attr/relation',
  )

// 属性分类初始化列表
export const reqAttrInitList = (
  attrGroupId: number,
  page: number,
  limit: number,
  key: string,
) =>
  request.get<any, any>(
    API.RELATION_LIST_URL +
      `/${attrGroupId}` +
      '/noattr/relation' +
      `?page=${page}&limit${limit}&key=${key}`,
  )

// 添加属性关联
export const reqAddRelationResponseData = (postData: PostData[]) => {
  return request.post<any, any>(API.RELATION_ADD_URL, postData)
}

// 移除关联
export const reqDelRelationResponseData = (data: any) => {
  return request.post<any, any>(API.RELATION_DEL_URL, data)
}
